//
//  MXMineTopicCollectVC.m
//  MXの衣柜
//
//  Created by 马霄 on 15/8/20.
//  Copyright (c) 2015年 霄 马. All rights reserved.
//
#define WIDTH ([UIScreen mainScreen].bounds.size.width)
#define HEIGHT ([UIScreen mainScreen].bounds.size.height)
#import "MXMineTopicCollectVC.h"
#import "MXFindContentCollectionViewCell.h"
#import "DataBaseFMDB.h"
#import "MXFindDBModel.h"
#import "UIImageView+WebCache.h"
#import "MXFindDetailVC.h"
#import "MXMineSingLabel.h"
#import "MXMineTopicListTableViewCell.h"
#import "UIView+Extension.h"
@interface MXMineTopicCollectVC ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *DataBaseArray;
@property (nonatomic, retain) NSMutableArray *contentArray;
@property (nonatomic, assign) BOOL isOpenEditModel;
@property (nonatomic, retain) UIButton *editButton;
@property (nonatomic, retain) NSMutableArray *buttonArray;
@property (nonatomic, retain) NSString *tempTitleString;
@end

@implementation MXMineTopicCollectVC
- (void)dealloc
{
    [_tempTitleString release];
    [_buttonArray release];
    [_editButton release];
    [_contentArray release];
    [_DataBaseArray release];
    [_collectionView release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;
    self.title = @"专题收藏";
    self.tabBarController.tabBar.hidden = YES;
    
    MXMineSingLabel *singLabel = [MXMineSingLabel singhLabel];
    
    [self.view addSubview:singLabel];
    
    
    DataBaseFMDB *dbManger = [[[DataBaseFMDB alloc] init] autorelease];
    
    // 创建数据库
    [dbManger createTopicDataBase];
    
    // 创建表
    [dbManger creatTopicDaseTable];
    
    
    self.DataBaseArray = [dbManger selectTopicData];
    
    
    if (self.DataBaseArray.count == 0) {
        singLabel.hidden = NO;
    } else {
        singLabel.hidden = YES;
    }
    
    [self.collectionView reloadData];
    
}

- (void)viewWillDisappear:(BOOL)animated
{
//    self.tabBarController.tabBar.hidden = NO;
    [self.collectionView reloadData];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.buttonArray = [NSMutableArray array];
    
    
//    UIButton *editButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
//    editButton.width = 60;
//    editButton.height = 30;
//    [editButton setTitle:@"编辑" forState:(UIControlStateNormal)];
//    [editButton setTitle:@"完成" forState:(UIControlStateSelected)];
//    editButton.backgroundColor = [UIColor redColor];
//    [editButton addTarget:self action:@selector(editAction:) forControlEvents:(UIControlEventTouchUpInside)];
//    
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:editButton];
    
    self.DataBaseArray = [NSMutableArray array];
    self.contentArray = [NSMutableArray array];
    
    DataBaseFMDB *dbManger = [[[DataBaseFMDB alloc] init] autorelease];
    
    // 创建数据库
    [dbManger createTopicDataBase];
    
    // 创建表
    [dbManger creatTopicDaseTable];
    
    
    
    self.DataBaseArray = [dbManger selectTopicData];
    
    [self creatCollectionView];
    
}


#pragma mark - FindTabBar设置CollectionView
- (void)creatCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    flowLayout.itemSize = CGSizeMake(WIDTHPRO * 170, HEIGHTPRO * 210);
    flowLayout.minimumLineSpacing = 10;
    flowLayout.sectionInset = UIEdgeInsetsMake(WIDTHPRO * 10 , HEIGHTPRO * 10 , 0, HEIGHTPRO * 10);
    
    self.collectionView = [[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:flowLayout] autorelease];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
    
    
    [self.collectionView registerClass:[MXMineTopicListTableViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
}

/************************* CollectionView Delegate **************************/

//Collection中的组别个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

//每个组别中元件的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.DataBaseArray.count;
    
}

//重用机制
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MXMineTopicListTableViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    cell.layer.masksToBounds = YES;
    
    
    
//    NSInteger dex = 0;
//    
////    for (int i = 0; i < self.DataBaseArray.count; i++) {
//    
//        
//        UIButton * editButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
//        editButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
//        
//        editButton.frame = CGRectMake(WIDTHPRO * 20, 0, WIDTHPRO * 20, HEIGHTPRO * 20);
//        
//        editButton.backgroundColor = [UIColor redColor];
//        
//        editButton.tag = dex;
//        
//        [editButton addTarget:self action:@selector(clickAction:) forControlEvents:(UIControlEventTouchUpInside)];
//        
//        editButton.hidden = NO;
//        
//        [self.buttonArray addObject:editButton];
//        
//        dex++;
    
//    }
    


//    [cell getInfo:self.DataBaseArray[indexPath.row] edit:self.isOpenEditModel row:indexPath.row];
    
    
//    [cell addSubview:_buttonArray[indexPath.row]];
    
    [cell.mainImageView sd_setImageWithURL:[NSURL URLWithString:[self.DataBaseArray[indexPath.row] imagePath]]];
    
    cell.detailLabel.text = [self.DataBaseArray[indexPath.row] detail];
    
//    cell.deleteButton.hidden = NO;
    
    
    
    
    return cell;
    
    
}


//collectionView 点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MXFindDetailVC *detailVC = [[[MXFindDetailVC alloc] init] autorelease];
    detailVC.AlbumID = [self.DataBaseArray[indexPath.row] AlbumID];
    detailVC.TypeID = [self.DataBaseArray[indexPath.row] TypeID];
    detailVC.TempImage = [self.DataBaseArray[indexPath.row] imagePath];
    detailVC.TempTitleString = [self.DataBaseArray[indexPath.row] detail];
    
//    self.tempTitleString = [self.DataBaseArray[indexPath.row] detail];
    
    [self.navigationController pushViewController:detailVC animated:YES];
}


-(void)removeCollectionViewAtIndexes:(NSIndexSet *)indexes
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
